<template>
  <div class="layui-container fly-marginTop">
    <div class="fly-panel fly-panel-user" pad20>
      <div class="layui-tab layui-tab-brief">
        <ul class="layui-tab-title">
          <li>
            <router-link :to="{name: 'login'}">登入</router-link>
          </li>
          <li class="layui-this">找回密码</li>
        </ul>
        <div class="layui-form layui-tab-content" style="padding: 20px 0;">
          <div class="layui-tab-item layui-show">
            <div class="layui-form layui-form-pane">
              <validation-observer ref="observer" tag="form" @submit.prevent="submit()">
                <div class="layui-form-item">
                  <label for="L_username" class="layui-form-label">用户名</label>
                  <ValidationProvider rules="required|email" name="用户名" v-slot="{errors}">
                    <div class="layui-input-inline">
                      <input
                        id="L_username"
                        type="text"
                        name="username"
                        v-model="username"
                        autocomplete="off"
                        class="layui-input"
                        placeholder="请输入用户名"
                      />
                    </div>
                    <div class="error layui-form-mid">{{errors[0]}}</div>
                  </ValidationProvider>
                </div>
                <div class="layui-form-item">
                  <label for="L_name" class="layui-form-label">昵称</label>
                  <ValidationProvider rules="required" name="昵称" v-slot="{errors}">
                    <div class="layui-input-inline">
                      <input
                        id="L_name"
                        type="text"
                        name="name"
                        v-model="name"
                        autocomplete="off"
                        class="layui-input"
                        placeholder="请输入昵称"
                      />
                    </div>
                    <div class="error layui-form-mid">{{errors[0]}}</div>
                  </ValidationProvider>
                </div>
                <div class="layui-form-item">
                  <label for="L_vercode" class="layui-form-label">验证码</label>
                  <ValidationProvider rules="required|length:4" name="code" v-slot="{errors}">
                    <div class="layui-input-inline">
                      <input
                        id="L_vercode"
                        type="text"
                        name="vercode"
                        placeholder="请输入验证码"
                        autocomplete="off"
                        class="layui-input"
                        v-model="code"
                      />
                    </div>
                    <div>
                      <span class="code" v-html="svg" @click="_getCaptcha"></span>
                    </div>
                    <div class="layui-form-mid">
                      <span class="error">{{errors[0]}}</span>
                    </div>
                  </ValidationProvider>
                </div>
                <div class="layui-form-item">
                  <input type="hidden" name="username" value />
                  <input type="hidden" name="email" value />
                  <button class="layui-btn" :disabled="invalid">提交</button>
                </div>
              </validation-observer>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { getCaptcha } from '@/api/login'
import { ValidationProvider, ValidationObserver } from 'vee-validate'
import { forget } from '@/api/login'
export default {
  name: 'forget',
  components: {
    ValidationObserver,
    ValidationProvider
  },
  data() {
    return {
      username: '',
      name: '',
      code: '',
      svg: '',
      invalid: false
    }
  },
  mounted() {
    this._getCaptcha()
  },
  methods: {
    async _getCaptcha() {
      let sid = this.$store.state.sid
      const res = await getCaptcha(sid)
      if (res.code === 200) {
        this.svg = res.data
      }
    },
    async submit() {
      this.invalid = await this.$refs.observer.validate()
      if (!this.invalid) {
        return
      }
      const result = await forget({
        name: this.name,
        username: this.username,
        code: this.code
      })
      if (result.code === 200) {
        this.$alert('邮件发送成功')
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.error {
  color: red;
}
</style>